
class_name Scene
extends Node2D




## 地图
@onready var map: SceneMap = $Map
## 部署点
@onready var deploy: SceneDeploy = $Deploy
## 单位管理器
@onready var unit_manage: SceneUnitManage = $UnitManage
## 摄像机
@onready var camera = $Camera
## 选择区域管理器
@onready var select_manage: SceneSelectManage = $SelectManage
## 控制权管理器
@onready var controller_manage: SceneControllerManage = $ControllerManage
## 指针
@onready var pointer: ScenePointer = $Pointer
## UI 面板
@onready var ui: SceneUI = $UI

enum SceneMode {
	## 一般模式
	NONE,
	## ui 模式
	UI,
	## 设置模式
	SETTING
}

## 场景模式
var scene_mode: SceneMode = SceneMode.NONE




func _ready() -> void:
	pointer.pointer_callable_signal.connect(pointer_callable)
	pointer.pointer_move_signal.connect(pointer_move)
	
	unit_manage.sort_signal.connect(ui.queue_plan.update)
	unit_manage.round_settlement_signal.connect(ui.game_info_plan.round_update)
	
	# 出击的信号连接
	ui.prepare_plan.depart_signal.connect(func () -> void:
		# 隐藏人员挑选UI
		ui.hide_prepare_plan()
		# 重置人员挑选UI的一些属性
		ui.prepare_plan.reset()
		for unit in deploy.get_units():
			# 设置 unit 们的控制权给玩家
			GData.player.add_unit(unit)
			unit_manage.add_child(unit)
		# 隐藏部署位
		deploy.hide()
		# 开始战斗
		start()
	)
	
	# 连接 UI 的单位队列ui信号
	ui.prepare_plan.add_unit_signal.connect(deploy.add_unit)
	ui.prepare_plan.remove_unit_signal.connect(deploy.remove_unit)
	
	# ui 设置 unit 备选
	var units: Array[ Unit ] = create_units()
	ui.prepare_plan.start(units)
	# 启动 战备 界面
	# 并等待它关闭
	ui.show_prepare_plan()




## 创建 Unit 以提玩家挑选
func create_units() -> Array[Unit]:
	var us: Array[Unit] = []
	for index in range(0, 12):
		var unit: Unit = null
		match index:
			0, 1:
				unit = preload("res://game/units/spear/spear.tscn").instantiate()
			2, 3:
				unit = preload("res://game/units/magician/magician.tscn").instantiate()
			4, 5:
				unit = preload("res://game/units/doctor/doctor.tscn").instantiate()
			6, 7:
				unit = preload("res://game/units/shooter/shooter.tscn").instantiate()
			8, 9:
				unit = preload("res://game/units/assassin/assassin.tscn").instantiate()
			10, 11:
				unit = preload("res://game/units/soldier/soldier.tscn").instantiate()
		add_child(unit)
		remove_child(unit)
		us.push_back(unit)
	return us




## 开始游戏调用
func start() -> void:
	ui.show_queue_plan()
	ui.show_game_info_plan()
	unit_manage.start()




## 结束游戏调用
func end(_camp: String) -> void:
	ui.show_result_plan(_camp)
	await ui.result_plan.end_siganl
	# 清除 player 的 unit
	GData.player.clear()
	get_tree().change_scene_to_file("res://game/main.tscn")




## 指针调用
func pointer_callable(_pointer: ScenePointer) -> void:
	# 判断 select_manage 是否在使用中
	if select_manage.await_handle:
		select_manage.pointer_callable(_pointer)
		return
	# 判断 deploy 是否在使用中
	if deploy.visible:
		# 显示 unit 区域
		var unit_call: Unit = unit_manage.pointer_callable(_pointer)
		if unit_call != null:
			unit_manage.unit_show_scope(unit_call)
			# 重置 deploy
			deploy.reset()
			return
		# 调整
		deploy.pointer_callable(_pointer)
	else:
		# 掉用 map 与 unit_manage 的 pointer_callable
		var map_call: Dictionary = map.pointer_callable(_pointer)
		var unit_call: Unit = unit_manage.pointer_callable(_pointer)
		if unit_call != null:
			unit_call.pointer_callable()




## 指针移动
func pointer_move(_pointer: ScenePointer) -> void:
	var map_call: Dictionary = map.pointer_move(_pointer)
	var unit_call: Unit = unit_manage.pointer_move(_pointer)
	select_manage.pointer_move(_pointer)
	# 显示 unit 信息
	if unit_call != null:
		ui.unit_info_plan.update(unit_call)
		ui.unit_info_plan.start()
	else:
		ui.unit_info_plan.end()
	# 显示图块的信息
	if not map_call.is_empty():
		ui.show_cell_info_plan(map_call)
	else:
		ui.hide_cell_info_plan()
